#!/usr/bin/python

from google.appengine.api import urlfetch
import re
import logging

class parser:

    @staticmethod
    def readContext(url):
        try:
            urlfetch.set_default_fetch_deadline(45)
            result = urlfetch.fetch(url)
            if result.status_code == 200:
                return result.content
            else:
                return None
        except urlfetch.InvalidURLError, e:
            logging.error(e)
            return None

    @staticmethod
    def getMatch(match):
        if match is None:
            return None
        r=""
        for m in match.groups():
            r+=m
        return r

    @staticmethod
    def printMatch(match):
        if match is None:
            print 'None'
        else:
            print '<Match: %r, groups=%r>' % (match.group(), match.groups())

    @staticmethod
    def getCharset(s):
        char_re = re.compile(r"(?is)content=[\"'].*?;\s*charset=(.*?)[\"']")
        return char_re.search(s)

    @staticmethod
    def testRE( p , url ):
        c = parser.readContext(url)
        print 'charset:'
        parser.printMatch(parser.getCharset(c))
        for l in p:
            print 'searching:<%r>' % l
            parser.printMatch(re.search( l , c ))

    def __init__(self, box):
        self.box=box

    def run(self):
        if self.box is None:
            raise TypeError,'Empty box'

        context = parser.readContext(self.box.url)

        for k in self.box.stuffs:
            b=re.compile(k.pattern)
            match=parser.getMatch(b.search(context))
            if match is None:
                logging.error("Bad pattern\"%s\" for url:<%s>", k.pattern, self.box.url)
            else:
                #logging.info("Got it <%s>", match)
                k.val=match

